package com.lianxi.Courses.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lianxi.Courses.constant.DictConstant;
import com.lianxi.Courses.constant.StatusConstant;
import com.lianxi.Courses.dao.CourseTypeMapper;
import com.lianxi.Courses.entity.CourseType;
import com.lianxi.Courses.entity.CourseTypeExample;
import com.lianxi.Courses.exception.CourseTypeExistException;
import com.lianxi.Courses.exception.RequestParameterErrorException;
import com.lianxi.Courses.service.CourseTypeService;

import com.lianxi.Courses.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class CourseTypeServiceImpl implements CourseTypeService {
    @Autowired
    private CourseTypeMapper typeMapper;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public List<CourseType> findAll() {
        return typeMapper.selectByExample(new CourseTypeExample());
    }


    // 实现查询没有子类的最小类别
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public List<CourseType> findLeafTypes() {
        List<CourseType> allTypes = findAll();
        List<CourseType> leafTypes = new ArrayList<>();

        for (CourseType type : allTypes) {
            try {

                List<CourseType> children = findByParentId(type.getId());
                if (children == null || children.isEmpty()) {
                    leafTypes.add(type);
                }
            } catch (RequestParameterErrorException e) {
                e.printStackTrace();
            }
        }

        return leafTypes;
    }




















    @Override
    public void addCourseType(String name, Integer parentId) throws RequestParameterErrorException, CourseTypeExistException {
        if (ParameterUtil.isNull(name)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        CourseTypeExample example = new CourseTypeExample();
        if (parentId != null) {
            example.or().andTypeNameEqualTo(name).andParentIdEqualTo(parentId);
        } else {
            example.or().andTypeNameEqualTo(name).andParentIdIsNull();
        }
        List<CourseType> types = typeMapper.selectByExample(example);
        if (!types.isEmpty()) {
            throw new CourseTypeExistException("该课程类型已经存在");
        }

        CourseType type = new CourseType();
        type.setTypeName(name);
        type.setStatus(StatusConstant.COURSE_TYPE_STATUS_ENABLED);
        type.setParentId(parentId);
        typeMapper.insertSelective(type);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<CourseType> findPage(String pageNo, String pageSize) {

        if (ParameterUtil.isNull(pageNo)) {
            pageNo = DictConstant.COURSE_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.COURSE_DEFAULT_PAGE_SIZE;
        }

        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));

        List<CourseType> types = typeMapper.selectByExample(new CourseTypeExample());

        PageInfo<CourseType> typePage = new PageInfo<>(types);
        return typePage;
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public CourseType findById(String id) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        return typeMapper.selectByPrimaryKey(Integer.parseInt(id));
    }

    @Override
    public void modifyName(String id, String name) throws RequestParameterErrorException, CourseTypeExistException {
        // 参数校验
        if (ParameterUtil.isNull(id) || ParameterUtil.isNull(name)) {
            throw new RequestParameterErrorException("请求参数有误");
        }

        CourseTypeExample example = new CourseTypeExample();
        example.or()
                .andTypeNameEqualTo(name)
                .andIdNotEqualTo(Integer.parseInt(id));
        List<CourseType> types = typeMapper.selectByExample(example);
        if (!types.isEmpty()) {
            throw new CourseTypeExistException("该课程类型已经存在");
        }

        CourseType type = new CourseType();
        type.setId(Integer.parseInt(id));
        type.setTypeName(name);
        typeMapper.updateByPrimaryKeySelective(type);

    }

    @Override
    public void modifyStatus(String id, String status) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id) || ParameterUtil.isNull(status)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        CourseType type = new CourseType();
        type.setId(Integer.parseInt(id));
        type.setStatus(Integer.parseInt(status));
        if (type.getStatus() == StatusConstant.COURSE_TYPE_STATUS_ENABLED) {
            type.setStatus(StatusConstant.COURSE_TYPE_STATUS_DISABLED);
        } else {
            type.setStatus(StatusConstant.COURSE_TYPE_STATUS_ENABLED);
        }
        typeMapper.updateByPrimaryKeySelective(type);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public List<CourseType> findByParentId(Integer parentId) throws RequestParameterErrorException {
        if (parentId == null) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        CourseTypeExample example = new CourseTypeExample();
        example.or().andParentIdEqualTo(parentId);
        return typeMapper.selectByExample(example);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<CourseType> findPageByParentId(String pageNo, String pageSize, String parentId) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(pageNo)) {
            pageNo = DictConstant.COURSE_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.COURSE_DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        CourseTypeExample example = new CourseTypeExample();
        if (ParameterUtil.isNull(parentId) || "null".equals(parentId)) {
            example.or().andParentIdIsNull();
        } else {
            example.or().andParentIdEqualTo(Integer.parseInt(parentId));
        }
        List<CourseType> types = typeMapper.selectByExample(example);
        PageInfo<CourseType> typePage = new PageInfo<>(types);
        return typePage;
    }

//    @Override
//    public List<CourseType> findTopLevelTypes() {
//        CourseTypeExample example = new CourseTypeExample();
//        example.or().andParentIdIsNull();
//        return typeMapper.selectByExample(example);
//    }|
@Override
public List<CourseType> findTopLevelTypes() {
    CourseTypeExample example = new CourseTypeExample();
    example.or().andParentIdIsNull();
    List<CourseType> topLevelTypes = typeMapper.selectByExample(example);

    // 递归加载所有级别的子分类
    for (CourseType type1 : topLevelTypes) {
        try {
            List<CourseType> type2List = findByParentId(type1.getId());
            type1.setChildren(type2List);

            // 加载二级分类的子分类
            for (CourseType type2 : type2List) {
                List<CourseType> type3List = findByParentId(type2.getId());
                type2.setChildren(type3List);
            }
        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
        }
    }

    return topLevelTypes;
}





}
















